import 'package:flutter/material.dart';
import '../l10n/app_localizations.dart';
import '../main.dart';

class TopBarWidget extends StatelessWidget {
  final bool isSmallScreen;
  final bool isSidebarCollapsed;
  final VoidCallback onToggleSidebar;
  final MembershipType currentMembership;
  final int enabledServicesCount;
  final VoidCallback onMembershipTap;

  const TopBarWidget({
    Key? key,
    required this.isSmallScreen,
    required this.isSidebarCollapsed,
    required this.onToggleSidebar,
    required this.currentMembership,
    required this.enabledServicesCount,
    required this.onMembershipTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: isSmallScreen ? 36 : 44,
      padding: EdgeInsets.symmetric(
        horizontal: isSmallScreen ? 8 : 12,
        vertical: isSmallScreen ? 4 : 6,
      ),
      decoration: const BoxDecoration(
        color: Color(0xFF2B3139),
        border: Border(
          bottom: BorderSide(
            color: Color(0xFF3C4043),
            width: 1,
          ),
        ),
      ),
      child: Row(
        children: [
          // 折叠按钮
          if (!isSidebarCollapsed)
            IconButton(
              icon: Icon(
                Icons.menu,
                color: Colors.white70,
                size: isSmallScreen ? 18 : 20,
              ),
              onPressed: onToggleSidebar,
              constraints: BoxConstraints(
                minWidth: isSmallScreen ? 32 : 36,
                minHeight: isSmallScreen ? 32 : 36,
              ),
              padding: EdgeInsets.all(isSmallScreen ? 4 : 6),
            ),
          SizedBox(width: isSmallScreen ? 8 : 12),
          // 标题
          Expanded(
            flex: 5,
            child: Text(
              AppLocalizations.of(context)!.appTitle,
              style: TextStyle(
                color: Colors.white,
                fontSize: isSmallScreen ? 16 : 20,
                fontWeight: FontWeight.bold,
              ),
              overflow: TextOverflow.ellipsis,
            ),
          ),
          // 会员状态显示
          if (MediaQuery.of(context).size.width > 800)
            Flexible(
              flex: 2,
              child: _buildMembershipBadge(context),
            )
          else if (MediaQuery.of(context).size.width > 500)
            Container(
              padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
              decoration: BoxDecoration(
                color: MembershipInfo.getInfo(currentMembership, context).color,
                borderRadius: BorderRadius.circular(4),
              ),
              child: Text(
                '$enabledServicesCount/${MembershipInfo.getInfo(currentMembership, context).maxServices}',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 10,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          SizedBox(width: isSmallScreen ? 8 : 12),
          // 开发者按钮
          if (MediaQuery.of(context).size.width > 600)
            Container(
              padding: EdgeInsets.symmetric(
                horizontal: isSmallScreen ? 8 : 12,
                vertical: isSmallScreen ? 4 : 6,
              ),
              decoration: BoxDecoration(
                color: Colors.blue.withOpacity(0.2),
                borderRadius: BorderRadius.circular(3),
                border: Border.all(
                  color: Colors.blue.withOpacity(0.5),
                  width: 1,
                ),
              ),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(
                    Icons.code,
                    color: Colors.blue,
                    size: isSmallScreen ? 14 : 16,
                  ),
                  SizedBox(width: 2),
                  Text(
                    AppLocalizations.of(context)!.developer,
                    style: TextStyle(
                      color: Colors.blue,
                      fontSize: isSmallScreen ? 12 : 14,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
            ),
        ],
      ),
    );
  }

  Widget _buildMembershipBadge(BuildContext context) {
    final membershipInfo = MembershipInfo.getInfo(currentMembership, context);

    return GestureDetector(
      onTap: onMembershipTap,
      child: Container(
        padding: EdgeInsets.symmetric(
          horizontal: isSmallScreen ? 4 : 8,
          vertical: isSmallScreen ? 2 : 4,
        ),
        decoration: BoxDecoration(
          color: membershipInfo.color,
          borderRadius: BorderRadius.circular(4),
        ),
        child: ClipRect(
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Icon(
                Icons.star,
                color: Colors.white,
                size: isSmallScreen ? 12 : 14,
              ),
              if (!isSmallScreen) SizedBox(width: 2),
              if (!isSmallScreen)
                 Flexible(
                   child: Text(
                     membershipInfo.displayName,
                     style: TextStyle(
                       color: Colors.white,
                       fontSize: 12,
                       fontWeight: FontWeight.w500,
                     ),
                     overflow: TextOverflow.ellipsis,
                   ),
                 ),
               if (!isSmallScreen) SizedBox(width: 2),
              Flexible(
                child: Text(
                  '($enabledServicesCount/${membershipInfo.maxServices})',
                  style: TextStyle(
                    color: Colors.white70,
                    fontSize: isSmallScreen ? 8 : 10,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}